package com.sgb.zhaocai.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.entity.CustomBaseBaen;
import com.sgb.zhaocai.widget.adapter.NewAddFieldAdapter;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/9/2
 * AUTHOR:F_Q
 * Description:项目管理新增字段
 */
public class NewAddFieldView extends RelativeLayout {

    private ImageView addCustomField;
    private RecyclerView recyclerview;
    private LinearLayout ll_add;
    private NewAddFieldAdapter newaddfieldadapter;
    private LinearLayoutManager layoutManager;

    private List<CustomBaseBaen> list;

    public NewAddFieldView(Context context) {
        this(context, null);
    }

    public NewAddFieldView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public NewAddFieldView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.zc_new_add_field_view, this);
        // 获取控件
        recyclerview = findViewById(R.id.recyclerview);//自定义字段的recyclerview
        addCustomField = findViewById(R.id.add_custom_field);//添加自定义字段界面
        ll_add = findViewById(R.id.ll_add);//添加自定义字段界面
        addCustomField.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加数据之前先判断数据是否都有值，处理ui布局
                dataProcessing(context);
                //添加view数据之后，处理ui布局
                addCustomFieldView();
            }
        });
        recyclerviewData(context);
    }

    //集合处理
    private void recyclerviewData(Context context) {
        list = new ArrayList<>();
        if (newaddfieldadapter == null) {
            newaddfieldadapter = new NewAddFieldAdapter(context, list);
        }
        layoutManager = new LinearLayoutManager(context);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setAdapter(newaddfieldadapter);
        newaddfieldadapter.setCustomfield(new NewAddFieldAdapter.FieldCallback() {
            @Override
            public void FieldDeleta(int position) {
                list.remove(position);
                //删除view数据之后，处理ui布局
                addCustomFieldView();
            }
        });
    }

    private void addCustomFieldView() {
        newaddfieldadapter.notifyDataSetChanged();
        recyclerview.scrollToPosition(list.size() - 1);
        if (list.size() >= 10) {
            ll_add.setVisibility(GONE);
        } else {
            ll_add.setVisibility(VISIBLE);
        }
    }

    //数据处理
    private void dataProcessing(Context context) {
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (TextUtils.isEmpty(list.get(i).getLabel())) {
                    MToast.showToast(context, "请完善第" + (i+1) + "条新增字段信息");
                    return;
                }
                if (TextUtils.isEmpty(list.get(i).getContext())) {
                    MToast.showToast(context, "请完善第" + (i+1) + "条新增字段信息");
                    return;
                }
            }
        }
        CustomBaseBaen bean = new CustomBaseBaen();
        list.add(bean);
    }

    //获取自定义控件中的数据信息
    public List<CustomBaseBaen> getCustomFieldViewData() {
        List<CustomBaseBaen> datas = newaddfieldadapter.getDatas();
        return datas;
    }

    /**
     * 设置数据
     * @param customBase
     */
    public void setData(List<CustomBaseBaen> customBase) {
        for (CustomBaseBaen customBaseBaen : customBase) {
            list.add(customBaseBaen);
        }
        //添加view数据之后，处理ui布局
        addCustomFieldView();
    }

}
